﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Deployment;
using System.Windows.Forms;
using Collector.LogServer;

namespace Collector.LogServer
{
    class Logger
    {
        StreamWriter logStreamWriter = null;
        string DirectoryPath = "";
        string logFileName = "";
        public int SubItemIndex = 0;
        string SerialNumber = "";
        string remoteFilePath = "";
        public Logger()
        {
            AppEvent.RegistEvent(EventId.TestStart, OnTestStart);
            AppEvent.RegistEvent(EventId.AddLog, OnAddLog);
            AppEvent.RegistEvent(EventId.TestEnd, OnTestEnd);
            AppEvent.RegistEvent(EventId.UpdateInfo, OnUpdateSN);
        }
        private void OnUpdateSN(object sender, AppEventArgs e)
        {
            if ((int)e.SubId != SubItemIndex)
            {
                return;
            }

            if(e.State != EventState.SN)
            {
                return;
            }

            SerialNumber = e.Data as string;
        }
        private void OnTestStart(object sender, AppEventArgs e)
        {
            if ((int)e.SubId != SubItemIndex)
            {
                return;
            }

            SerialNumber = e.Data as string;

            string tools_path = System.Windows.Forms.Application.StartupPath + "\\";
            string strdate = DateTime.Now.ToString("yyyy-MM-dd");
            DirectoryPath = tools_path + String.Format("Log\\{0}\\DUT{1}", strdate, SubItemIndex);
            remoteFilePath = String.Format("Log\\{0}\\DUT{1}", strdate, SubItemIndex);
            logFileName = String.Format("DUT{0}_{1}_{2}.txt", SubItemIndex, strdate, DateTime.Now.TimeOfDay.ToString("hhmmssfff"));
            if (!Directory.Exists(DirectoryPath))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(DirectoryPath);
                directoryInfo.Create();
            }
            File.Delete(DirectoryPath + "\\" + logFileName);
            logStreamWriter = new StreamWriter(DirectoryPath + "\\" + logFileName);

        }



        public void OnAddLog(object sender, AppEventArgs e)
        {
            if ((int)e.SubId != SubItemIndex)
            {
                return;
            }

            string Msg = "";

            switch (e.State)
            {
                case EventState.LOG_INFO:
                    Msg = e.Data as string;
                    if (null == Msg || Msg.Length <= 0)
                    {
                        return;
                    }
                    break;
                case EventState.LOG_ITEM:
                    {
                        UIItemInfo uiinfo = e.Data as UIItemInfo;
                        Msg = uiinfo.MakeString();
                    }
                    break;
                default:
                    //do nothing;
                    {
                        return;
                    }
            }

            Msg = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] " + Msg.Trim();

            if (null != logStreamWriter)
            {
                logStreamWriter.WriteLine(Msg.Trim());
                logStreamWriter.Flush();
            }
        }

        public void PrintLog(string Msg)
        {
            Msg = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] " + Msg.Trim();

            if (null != logStreamWriter)
            {
                logStreamWriter.WriteLine(Msg.Trim());
                logStreamWriter.Flush();
            }
        }

        private void OnTestEnd(object sender, AppEventArgs e)
        {
            if ((int)e.SubId != SubItemIndex)
            {
                return;
            }

            bool bpass = (FormMain.RunStatus.PASS == (FormMain.RunStatus)e.Data);
            try
            {
                if (logStreamWriter != null)
                {
                    logStreamWriter.Flush();
                    logStreamWriter.Close();
                    logStreamWriter = null;

                    //bool bpass = //(MainForm.RunStatus)e.Data == MainForm.RunStatus.PASS;

                    FileInfo fi = new FileInfo(DirectoryPath + "\\" + logFileName);
                    string[] arr = logFileName.Split(new string[] { ".txt" }, StringSplitOptions.RemoveEmptyEntries);

                    fi.MoveTo(DirectoryPath + "\\" + arr[0] + "_" + SerialNumber + (bpass ? "_PASS.txt" : "_FAIL.txt"));

                    string fp = "\\" + arr[0] + "_" + SerialNumber + (bpass ? "_PASS.txt" : "_FAIL.txt");
                    string remoteTarget = remoteFilePath + fp;
                    string locaFile = DirectoryPath + fp;

                    if (!FtpSever.uploaderFileToFtp(remoteTarget, locaFile))
                    {
                        MessageBox.Show(String.Format("Uploader File Fail.. Local : {0}, Remote:{1} ", locaFile, remoteTarget));
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

    }
}
